package priv.conceit.sc.hoxton.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import priv.conceit.sc.hoxton.common.entity.file.qo.FileUploadQO;
import priv.conceit.sc.hoxton.common.web.WebResponse;
import priv.conceit.sc.hoxton.file.service.FileService;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    FileService fileService;

    /**
     * upload文件上传
     *
     * @param fileUploadQO
     * @return priv.conceit.sc.hoxton.common.util.RestfulResponse
     * @author conceit
     * @date 2020/4/27 15:21
     */
    @PostMapping("upload")
    public WebResponse upload(@Valid FileUploadQO fileUploadQO){
        return WebResponse.build(fileService.upload(fileUploadQO));
    }


    /**
     * download 文件下载
     *
     * @param shortCode 文件短编码
     * @return org.springframework.http.ResponseEntity
     * @author conceit
     * @date 2020/4/27 15:21
     */
    @GetMapping("download/{shortCode}")
    public ResponseEntity download(@NotBlank @PathVariable String shortCode) throws Exception {
        return fileService.download(shortCode);
    }

    /**
     * preview 文件预览
     *
     * @param shortCode
     * @return org.springframework.http.ResponseEntity
     * @author conceit
     * @date 2020/4/27 15:22
     */
    @GetMapping("preview/{shortCode}")
    public ResponseEntity preview(@NotBlank @PathVariable String shortCode){
        return fileService.preview(shortCode);
    }

    /**
     * share 文件分享
     * 本质就是带有过期时间的下载接口
     *
     * @param
     * @return priv.conceit.sc.hoxton.common.util.RestfulResponse
     * @author conceit
     * @date 2020/4/27 15:22
     */
    @GetMapping("share/{shortCode}")
    public WebResponse share(@NotBlank @PathVariable String shortCode){
        return fileService.share(shortCode);
    }

    /**
     * del 文件删除
     *
     * @param shortCode 文件短编码
     * @return priv.conceit.sc.hoxton.common.util.RestfulResponse
     * @author conceit
     * @date 2020/4/27 15:22
     */
    @DeleteMapping("del/{shortCode}")
    public WebResponse del(@NotBlank @PathVariable String shortCode){
        return WebResponse.build(fileService.del(shortCode));
    }

}
